﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Models;
using System.Text;

public partial class Admin_Repair_AssignPower : System.Web.UI.Page
{
    Entities db = new Entities();
    private int Id = 0;
    public string device_power = "", _ID = "";
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Request.QueryString["Id"].ToString() != "0")
        {
            _ID = Request.QueryString["Id"].ToString();
            Id = int.Parse(_ID);
        }
        if (!IsPostBack)
        {
            //SortBind();
            if (Id != 0) { myDataBind(); }
        }
        GetMenuString();
    }

    private void myDataBind()
    {
        var _Store = db.S_Goods.SingleOrDefault(p => p.GID == Id);
        ddrParent.Text = _Store.GName;
    }


    protected void GetMenuString()
    {
        List<LeftMenu> menus = LeftMenu.Load();
        StringBuilder sb = new StringBuilder();
        Entities db = new Entities();
        var _Stores = db.S_Store;
        foreach (var _Store in _Stores)
        {
            sb.AppendFormat("<ul class=\"sec_menu\" menuid=\"{0}\">\r\n", _Store.SID);
            sb.Append("<li  class=\"li_menu\" style=\"float:left;margin-right: 10px;width:width: 33.3%;;list - style - type: none;\"><input type=checkbox  name=\"device_power\" class=\"checkall checkbox-inline i-checks\"  value=\"");
            sb.Append(_Store.SID + "\"");
            if (Id != 0) //
            {
                if (!string.IsNullOrEmpty(_Store.SPowerSort) && _Store.SPowerSort.IndexOf("," + _ID + ",") > -1)//如果工程师设备权限里已经有该设备
                    sb.Append(" checked");
            }
            sb.Append("/>");
            sb.AppendFormat(" {0} </li>\r\n", _Store.Sname + "(" + _Store.SGuid + ")");
        }
        sb.Append("</ul><br>\r\n");
        // }
        ltMasterSetting.Text = sb.ToString();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Request.Form["device_power"] != null)
            device_power = "," + Request.Form["device_power"].ToString() + ",";//,76,78,选择何靖 陈家财
        string[] En = device_power.Split(new char[] { ',' },StringSplitOptions.RemoveEmptyEntries);

        var Stores = db.S_Store;//找出所有工程师的ID
        foreach (var Store in Stores)
        {
            var changerStore = db.S_Store.Single(x => x.SID == Store.SID);

            if (device_power.IndexOf("," + Store.SID + ",") > -1)//选中工程师
            {
                if (string.IsNullOrEmpty(Store.SPowerSort) || Store.SPowerSort.IndexOf("," + _ID + ",") == -1)//如果该工程师不含有该设备ID
                {
                    if (string.IsNullOrEmpty(Store.SPowerSort))
                    {
                        changerStore.SPowerSort += "," + _ID + ",";
                    }
                    else
                    {
                        changerStore.SPowerSort += _ID + ",";
                    }

                    db.SaveChanges();
                    Response.Write("<script> var success = 1; </script>");
                }
            }
            else//未中工程师
            {
                var ii = Store.SPowerSort.IndexOf("," + _ID + ",");
                if (Store.SPowerSort.IndexOf("," + _ID + ",") > -1)//如果该工程师已经含有该设备ID
                {

                    string tring = _ID + ",";
                    //删除工程师ID
                    changerStore.SPowerSort = changerStore.SPowerSort.Replace(tring, "");
                    db.SaveChanges();
                    Response.Write("<script> var success = 1; </script>");
                }

            }
        }
    }
}